package com.simin.base.appdialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.LayoutRes;

import com.simin.base.R;

/**
 * date:2023/9/25
 * author:wsm
 * faucation:屏幕中间弹窗
 */
public class CenterDialogManager {

    /**
     * show弹窗dialog
     *
     * @param context 上下文
     * @return
     */
    public static AlertDialog buildCenterDialog(final Activity context, @LayoutRes int layoutId, OnBindViewCallback onBindViewCallback) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.DialogStyle);
        View view = View.inflate(context, layoutId, null);
        builder.setView(view);
        AlertDialog dialog = builder.create();
        WindowManager m = context.getWindowManager();
        Display d = m.getDefaultDisplay();  //为获取屏幕宽、高
        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();  //获取对话框当前的参数值、
        params.width = (int) (d.getWidth());    //宽度设置全屏宽度
        dialog.getWindow().setAttributes(params);     //设置生效
        if (onBindViewCallback != null) {
            onBindViewCallback.onBindView(view, dialog);
        }
        if (!context.isFinishing()) {
            dialog.show();
        }
        return dialog;
    }

    public static AlertDialog buildCenterDialog(final Activity context, @LayoutRes int layoutId, boolean isOutside, OnBindViewCallback onBindViewCallback) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.DialogStyle);
        View view = View.inflate(context, layoutId, null);
        builder.setView(view);
        AlertDialog dialog = builder.create();
        WindowManager m = context.getWindowManager();
        Display d = m.getDefaultDisplay();  //为获取屏幕宽、高
        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();  //获取对话框当前的参数值、
        params.width = (int) (d.getWidth());    //宽度设置全屏宽度
        dialog.getWindow().setAttributes(params);     //设置生效
        if (onBindViewCallback != null) {
            onBindViewCallback.onBindView(view, dialog);
        }
        if (!context.isFinishing()) {
            dialog.show();
        }
        dialog.setCanceledOnTouchOutside(isOutside);
        return dialog;
    }

    public interface OnBindViewCallback {
        void onBindView(View view, Dialog dialog);
    }

}
